/**
 * 文件：ConDataBasePanel.java
 * 说明：用于数据库
 * 作者：古通基
 * 时间：2011-09-12
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ConDataBasePanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -2099146202005738082L;
	private JLabel name = new JLabel("数据库名：");                                      //静态标签
	private JLabel user = new JLabel("用  户  名：");
	private JLabel pass = new JLabel("用户密码：");              
	
	private static JTextField jname = new JTextField("请输入数据库名",10);               //文本输入
	private static JTextField juser = new JTextField("请输入数据库用户名",10);          //文本输入
	private static JTextField jpass = new JTextField("请输入数据库用户密码",10);      //文本输入
                      
    private JButton connect = new JButton("连接");                                     //声明按钮
    private JButton reset = new JButton("重置");   
    
    private JPanel JPcomon1 = new JPanel();                                               //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public ConDataBasePanel()                                                                    //构造函数
    {
    	name.setHorizontalAlignment(SwingConstants.CENTER);
    	user.setHorizontalAlignment(SwingConstants.CENTER);
    	pass.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(name);
    	JPcomon1.add(jname);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(user);
    	JPcomon2.add(juser);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(pass);
    	JPcomon3.add(jpass);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(connect);
    	JPbutton.add(reset);
    	
    	this.setLayout(new FlowLayout(FlowLayout.CENTER,12,20));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPbutton);
    	
    	connect.addActionListener(this);                                                        //事件监听        
    	reset.addActionListener(this);
    	jname.addMouseListener(this);
    	juser.addMouseListener(this);
    	jpass.addMouseListener(this);
    }    
    
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("连接"))
		{
			getDataBaseName();
			getUser();
			getPass();
			try 
			{
				BaseSQL sqltest = new BaseSQL();
				sqltest.stmt.getConnection();
				JOptionPane.showMessageDialog(this, "恭喜你，连接数据库成功啦！", "系统消息",JOptionPane.INFORMATION_MESSAGE);
				sqltest.stmt.close();
				sqltest.con.close();
			} 
			catch(SQLException evt)
			{
				JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
			} 
			catch (ClassNotFoundException evt) 
			{
				JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
			}
			catch (NumberFormatException evt) 
			{
				JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
			}	
		}
		else if(e.getActionCommand().equals("重置"))
		{
			jname.setText("");
			juser.setText(null);
			jpass.setText("");
			this.setVisible(true);
		}
	}	
	public static String getDataBaseName()
	{
		return jname.getText();
	}
	public static String getUser()
	{
		return juser.getText();
	}
	public static String getPass()
	{
		return jpass.getText();
	}	
    public void mouseClicked(MouseEvent e) { }                                //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(jname.getText().equals("请输入数据库名"))
    	{    		
    		jname.setText("");
    		this.setVisible(true);
    	}
    	else if(juser.getText().equals("请输入数据库用户名"))
    	{    		
    		juser.setText("");
    		this.setVisible(true);
    	}
    	else if(jpass.getText().equals("请输入数据库用户密码"))
    	{    		
    		jpass.setText("");
    		this.setVisible(true);
    	}
    }  
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }  
}